package com.hsylient.javase.chapter01;

import org.junit.Test;

import java.util.Arrays;

public class MyArrayTest {
    @Test
    public void arrayInit() {
        // 静态初始化
        Integer[] integers1 = new Integer[]{1, 23, 4, 4, 5};
        // 动态初始化
        Integer[] integers = new Integer[12];
        // 自动类型推断
        Integer[] intArray = {1, 2, 3, 4, 5};
    }

    @Test
    public void arrayEvaluate() {
        int[] intArray = new int[10];
        intArray[0] = 0;
        intArray[1] = 1;
        intArray[2] = 2;
        intArray[3] = 3;
        intArray[4] = 4;
        intArray[5] = 5;
        for (int anInt : intArray) {
            System.out.println(anInt);
        }
    }

    /**
     * 数组字段
     */
    @Test
    public void arrayField() {
        int[] intArray = new int[10];
        System.out.println(intArray.length);
    }

    /**
     * 常见的数组下标越界异常
     */
    @Test
    public void arrayOutOfBounds() {
        String[] strings = {"Hsylient", "Syy"};
        System.out.println(strings.length);
        System.out.println(strings[2]);
    }

    /**
     * 二维数组的循环遍历
     */
    @Test
    public void array2d() {
        Integer[][] integers = {{1, 2}, {3, 4}, {5, 6}};
        for (Integer[] integer : integers) {
            for (Integer value : integer) {
                System.out.println(value);
            }
        }
    }

    /**
     * 数组的拷贝
     */
    @Test
    public void arrayCopy() {
        String[] resource = {"syy", "hsylient", "hello"};
        String[] target = new String[resource.length];
        for (int i = 0; i < target.length; i++) {
            target[i] = resource[i];
        }
        System.out.println(Arrays.toString(target));
    }

    /**
     * 数组的倒序输出
     */
    @Test
    public void arrayReverse() {
        String[] resource = {"syy", "hsylient", "hello"};
        for (int index = resource.length - 1; index >= 0; index--) {
            System.out.println(resource[index]);
        }
    }

    /**
     * 二分法查找
     */
    @Test
    public void searchBinary() {
        int target = 10;
        int[] resource = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int heard = 0;
        int end = resource.length - 1;
        while (heard <= end) {
            int mid = (heard + end) / 2;
            if (target == resource[mid]) {
                System.out.println("找到了，下标为：" + mid);
                break;
            } else if (target > resource[mid]) {
                heard = mid + 1;
            } else {
                end = mid - 1;
            }
        }
    }

    /**
     * 冒泡排序
     *
     */
    @Test
    public void BubbleSort() {
        int[] resource = {43, 32, 76, -98, 0, 64, 33, -21, 32, 99};
        // 这一层是将要交换的次数
        for (int i = 0; i < resource.length - 1; i++) {
            // 每一次都是一次从前到后的两两比较
            for (int j = 0; j < resource.length - i - 1; j++) {
                if (resource[j] > resource[j + 1]) {
                    resource[j] = resource[j] + resource[j + 1];
                    resource[j + 1] = resource[j] - resource[j + 1];
                    resource[j] = resource[j] - resource[j + 1];
                }
            }
        }
        System.out.println(Arrays.toString(resource));
    }
}
